{include file='public/meta'}
<title>模型管理 - 系统模型 - 木鱼内容管理系统</title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div style="padding: 10px; background-color: #FAFAFA;">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md13">
                    <div class="layui-card">
                        <div class="layui-card-header muyu-text">操作台&nbsp; &nbsp; &nbsp;<span class="muyu-tips-right">Tips:删除模型后,对应的所有信息将不复存在!</span></div>
                        <div class="layui-card-body" style="display:inline-block;">
                            <span style="display:inline-block;">选中项：</span>
                            <div class="layui-btn-container" style="display:inline-block;">
                                <a class="layui-btn layui-btn-xs layui-btn-danger" onclick="moddel()" href="javascript:;">批量删除</a>
                            </div>
                        </div>
                        <div class="layui-card-body" style="display:inline-block;">
                            <span style="display:inline-block;">其他操作：</span>
                            <div class="layui-btn-container" style="display:inline-block;">
                                <a class="layui-btn layui-btn-xs" onclick="openmodel('添加模型','{:url('admin/Model/add')}')" href="javascript:;">添加模型</a>
                                <a class="layui-btn layui-btn-xs layui-btn-normal" onclick="" id="impmod" href="javascript:;">导入模型</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div style="padding: 10px 15px 20px 15px;">
            <span style="float:left;">共有模型：<span style="color:red;">{$list|count}</span> 个</span>
        </div>
        <div class="layui-card-body">
            <div class="layui-table-body layui-table-main">
                <table class="layui-table" id="LAY-user-front-submit">
                    <thead>
                    <tr>
                        <th class=" layui-table-col-special">
                            <div class="layui-table-cell laytable-cell-checkbox">
                                <input type="checkbox" id="checkAll" onclick="isCheck()">
                            </div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>id</span></div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>模型名称</span></div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>模型标识</span></div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>模型简介</span></div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>模型类型</span></div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>创建时间</span></div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>状态</span></div>
                        </th>
                        <th>
                            <div class="layui-table-cell" align="center"><span>操作</span></div>
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    {foreach $list as $val}
                    <tr align="center">
                        <td class="layui-table-col-special">
                            <input type="checkbox" name="fav" value="{$val.id}">
                        </td>
                        <td>{$val.id}</td>
                        <td><span style="{eq name='$val.status' value='0'}color:red;{/eq}">{$val.name}</span></td>
                        <td>{$val.tablename}</td>
                        <td>{$val.intro}</td>
                        <td><div class="layui-btn layui-btn-xs layui-btn-{php}echo $val['type'] == '1' ? 'normal' : 'warm';{/php}">{php}echo $val['type'] == '1' ? '全局' : '内部';{/php}</div></td>
                        <td>{$val.create_time|date='Y:m:d'}</td>
                        <td><div class="layui-table-cell">
                            <a class="layui-btn-xs layui-btn layui-btn-{php}echo $val['status'] == '1' ? 'btn' : 'danger';{/php}" onclick="modelst('{$val.id}')" href="javascript:;"><i class="layui-icon layui-icon-face-{php}echo $val['status'] == '1' ? 'smile' : 'cry';{/php}"></i>{php}echo $val['status'] == '1' ? '启用' : '禁用';{/php}</a>
                        </div>
                        </td>
                        <td align="center"  class="layui-table-col-special">
                            <div class="layui-table-cell">
                                <a class="layui-btn layui-btn-xs" onclick="openmodel('模型编辑','{:url('admin/Model/edit',['id'=>$val.id])}')" href="javascript:;">编辑</a>
                                <a class="layui-btn layui-bg-cyan layui-btn-xs" onclick="openmodel('字段管理','{:url('admin/Modelfield/index',['moid'=>$val.id])}')" href="javascript:;">字段管理</a>
                                <a class="layui-btn layui-btn-xs layui-btn-warm" onclick="moddel('{$val.id}')" href="javascript:;">删除</a>
                                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue" onclick="educemodl('{$val.id}','{$val.tablename}')" href="javascript:;">导出</a>
                            </div>
                        </td>
                    </tr>
                    {/foreach}
                    </tbody>
                </table>
            </div>
            <div class="layui-box layui-laypage layui-laypage-molv">{$list|raw}</div>
        </div>

    </div>


    <!-- 低部区域 -->
    {include file='public/footer'}
    <script>
        function isCheck(){
            var IsCheck= document.getElementById("checkAll");
            if(IsCheck.checked==false){
                var groupC= document.getElementsByName("fav");
                for(var i=0;i<groupC.length;i++){
                    groupC[i].checked=false;
                }
            }
            if (IsCheck.checked==true) {
                var groupC= document.getElementsByName("fav");
                for(var i=0;i<groupC.length;i++){
                    groupC[i].checked=true;
                }
            }
        }
        function modelst(id){
            layer.msg('确认要变更状态吗？', {
                time: 5000, //5s后自动关闭
                shade: 0.3,
                btn: ['确认', '取消'],
                yes: function() {
                    //执行变更
                    $.post("{:url('admin/Model/modelst')}",{id:id}, function(data) {
                        if(data.code === 1){
                            layer.msg(data.msg, {time: 2000}, function() {
                                location.reload();
                            });
                        }else{
                            layer.msg(data.msg, {time: 2000});
                        }
                    })
                }
            });
        }
        function openmodel(title,url){
            layer.open({
                type: 2
                ,title: title
                ,content: url
                ,area: ['70%', '80%']
            });
        }
        function moddel(id){
            layer.msg('删除前请做好数据备份!', {
                time: 5000, //5s后自动关闭
                shade: 0.3,
                btn: ['确认', '取消'],
                yes: function() {
                    var aid=$("tbody input:checkbox");
                    var ids=[];
                    if(id == null){
                        id = allcheck(ids,aid,false);
                        if(id !=""){
                            id = id.substring(0, id.length - 1);
                        }
                    }
                    if(id !="" && id !=null){
                        $.post("{:url('admin/Model/moddel')}",{delid:id},function(data){
                            if(data.code === 1){
                                layer.msg(data.msg, {time: 2000}, function() {
                                    location.reload();
                                });
                            }else{
                                layer.msg(data.msg, {time: 2000});
                            }
                        });
                    }else{
                        layer.msg('当前未选择任何信息!',{icon:2,time:2000});
                    }
                }
            });
        }
        function educemodl(modid,tabname){
            if(modid == null || tabname == null){
                layer.msg("必要参数丢失!",{time:2000});
            }
            var Data = {modid:modid,tablename:tabname,tabst:"1"};
            layer.confirm('是否导出模型下内容数据？', {
                btn: ['导出','不导出','取消'] //按钮
            }, function(){
                toajax(Data);
            }, function(){
                Data = {modid:modid,tablename:tabname,tabst:"0"};
                toajax(Data);
            },function(){
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
        }
        function toajax(Data){
            $.ajax({
                type:'POST',
                data:Data,
                url:"{:url('admin/Model/educemodl')}",
                dataType :"json",
                beforeSend: function() {
                    index = layer.load();
                },
                success:function(data){
                    var urls = data.modurl;
                    if(data.code == 1){
                        layer.msg(data.msg,{time:2000},function(){
                            openDownloadDialog(urls,data.fiename);
                        });
                    }else{
                        layer.msg(data.msg,{time:2000});
                    }
                },
                complete:function(){
                    layer.close(index);
                }
            });
        }
        function openDownloadDialog(url, saveName)
        {
            if(typeof url == 'object' && url instanceof Blob)
            {
                url = URL.createObjectURL(url); // 创建blob地址
            }
            var aLink = document.createElement('a');
            aLink.href = url;
            aLink.download = saveName || ''; // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
            var event;
            if(window.MouseEvent) event = new MouseEvent('click');
            else
            {
                event = document.createEvent('MouseEvents');
                event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            }
            aLink.dispatchEvent(event);
        }

        layui.use('upload', function(){
            var upload = layui.upload;

            //执行上传
            var index;
            var uploadInst = upload.render({
                elem: '#impmod' //绑定元素
                ,url: "{:url('admin/Model/importdata')}" //上传接口
                ,field:'modata'
                ,accept:'file'
                ,exts:'model'
                ,before:function(muyu){
                    index = layer.load();

                }
                ,done: function(res){
                    //上传完毕回调
                    layer.close(index);
                    // console.log(res);
                    layer.msg(res.msg,{time:2000},function(){
                        if(res.code == 1){
                            location.reload();
                        }
                    });
                }
                ,error: function(upload){
                    //请求异常回调

                }
            });
        });
        function updatemodel(){
            $.ajax({
                type: 'POST',
                url: '{:url("admin/Model/updatemodel")}',
                dataType: "json",
                beforeSend: function() {
                    index = layer.load();
                },
                success: function(data) {
                    layer.msg(data.msg, {time: 2000}, function() {
                        location.reload();
                    });
                },
                complete: function() {
                    layer.close(index);
                }
            });
        }
    </script>
</body>
</html>